
var table;
//相关业务逻辑
$(function(){
	orderProcedure.bind();
	orderProcedure.getAllWorkShopAndProcedure();
	orderProcedure.getOrderProcedure();
	orderProcedure.removeBtnBind();
	orderProcedure.remove();
	
});


var orderProcedure={//页面总变量
		tempId:"",
		bind:function(){
			$("#addTemplate").bind("click",function(){
				$("input[type='checkbox']").prop("checked",false); 
				$("#addTemplateModal").modal("show");
			});
			
			//添加模板
			$("#addModalBtn").bind("click",function(){
				var name=$("#addName").val();
				if(name==""){
					layAlert("请输入模板名称");
					return false;
				}
				var procedureArray=new Array();
                //遍历所有车间
				var i=1;
				$(".right-box").find(".item").each(function(){
					var pro=new Object();
					pro.workshopName=$(this).attr("value");
					pro.processName=$(this).attr("data-id");
					pro.sort=i;
					procedureArray.push(pro);
					i++;
				});
				
				if(procedureArray.length<=0){
					layAlert("请选择工序");
					return false;
				}
                ajaxUtil.ajaxRuquest("orderProcedureTemplate/addTemplate",{"procedureArray":JSON.stringify(procedureArray),"name":name,"type":"create"},function(data){
					layer.msg(data,{icon:1});
					table.ajax.reload( null, false);
					$("#addTemplateModal").modal("hide");
				});
			});
			
			//添加工序确定按钮
			$("#addProcedureModalBtn").bind("click",function(){
				var procedureArray=new Array();
                //遍历所有车间
				$('.addPorkShopCheck').each(function () {  
					var name=$(this).attr("name");
					var workValue=$(this).val();
					//如果车间下有选中的工序
					if($("input[name='proce"+name+"']:checked").length>0){
						$("input[name='proce"+name+"']:checked").each(function(){
							var pro =new Object();
							pro.workshopName=workValue;
							pro.processName=$(this).val();
							pro.sort=0;
							procedureArray.push(pro);
						});
					}
			    });
				if(procedureArray.length<=0){
					layAlert("请选择工序");
					return false;
				}
				 ajaxUtil.ajaxRuquest("orderProcedureTemplate/addTemplate",{"procedureArray":JSON.stringify(procedureArray),"tempId":orderProcedure.tempId,"type":"add"},function(data){
						layer.msg(data,{icon:1});
						table.ajax.reload( null, false);
						$("#addProcedureModal").modal("hide");
				 });
			});
			
			
			//删除按钮
			$('#orderProcedureTable tbody').on('click', '.delete', function () {
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("orderProcedureTemplate/deleteTemplate",{"id":id},function(data){
					layer.msg(data,{icon:1});
					table.ajax.reload( null, false);
				});
			});
			
			//添加按钮
			$('#orderProcedureTable tbody').on('click', '.add', function () {
				orderProcedure.tempId=$(this).val();
				orderProcedure.getTempNoHaveProcedure();
				$("#addProcedureModal").modal("show");
			});
			
			//点击移除按钮
			$('#orderProcedureTable tbody').on('click', '.remove', function () {
				var text=$(this).text();
				if(text=="移除工序"){
					$(this).parents().children('td').children('.deleteX').show();
					$(this).text("完成");
				}else{
					$(this).parents().children('td').children('.deleteX').hide();
					$(this).text("移除工序");
				}
			});
			
			
			//点击修改名称
			$('#orderProcedureTable tbody').on('click', '.update', function () {
				var text=$(this).text();
				if(text=="修改名称"){
					var name=$(this).parents().children('td').eq(0).text();
					$(this).parents().children('td').eq(0).html("<input class='input-text ' type='text' value='"+name+"' />");
//					$(this).parents().children('td').children('.deleteX').show();
					$(this).text("确认修改");
				}else{
					var tempId=$(this).val();
					var name=$(this).parents().children('td').eq(0).children('input').val();
					ajaxUtil.ajaxRuquest("orderProcedureTemplate/updateTempName",{"tempId":tempId,"name":name},function(data){
						layer.msg(data,{icon:1});
					    table.ajax.reload(null, false);
					});
				}
			});
			
			//点击x
			$('#orderProcedureTable tbody').on('click', '.deleteX', function () {
				var rowIdex= $(this).parent()[0]._DT_CellIndex.row; 
				var tempid=$(this).parents().children('td').children('.delete').val();
				var procedureId=$(this).attr("value");
				ajaxUtil.ajaxRuquest("orderProcedureTemplate/removeTemplateProcedure",{"tempId":tempid,"procedureId":procedureId},function(data){
					layer.msg(data,{icon:1});
				    table.ajax.reload(function(data){
				    	table.rows().nodes().flatten().to$().eq(rowIdex).children('td').children('.deleteX').show();
				    	table.rows().nodes().flatten().to$().eq(rowIdex).children('td').children('.remove').text("完成");
				    }, false);
				});
			});
		},
		checkBind:function(){
			$(".workShopCheck").bind("click",function(){
				var name=$(this).attr("name");
				var is=$(this).is(":checked");
				if(is){
					$("input[name='proce"+name+"']").prop("checked",true); 
				}else{
					$("input[name='proce"+name+"']").prop("checked",false); 
				}
			});
			
			$(".proceCheck").bind("click",function(){
				var name=$(this).attr("name");
				var fatherName=name.substring(5,name.length);
				var length=$("input[name='"+name+"']").length; 
				var checkLength=$("input[name='"+name+"']:checked").length; 
				if(length==checkLength){
					$("input[name='"+fatherName+"']").prop("checked",true); 
				}else{
					$("input[name='"+fatherName+"']").prop("checked",false)
				}
			});
		},
		addCheckBind:function(){
			$(".addPorkShopCheck").bind("click",function(){
				var name=$(this).attr("name");
				alert
				var is=$(this).is(":checked");
				if(is){
					$("input[name='proce"+name+"']").prop("checked",true); 
				}else{
					$("input[name='proce"+name+"']").prop("checked",false); 
				}
			});
			
			$(".addProceCheck").bind("click",function(){
				var name=$(this).attr("name");
				var fatherName=name.substring(5,name.length);
				var length=$("input[name='"+name+"']").length; 
				var checkLength=$("input[name='"+name+"']:checked").length; 
				if(length==checkLength){
					$("input[name='"+fatherName+"']").prop("checked",true); 
				}else{
					$("input[name='"+fatherName+"']").prop("checked",false)
				}
			});
		},
		getOrderProcedure:function(){
			table=ajaxUtil.dataTablesAjax("orderProcedureTable",10,"orderProcedureTemplate/getOrderProcedure",
					{},[
					{"data":"name"},
					{"data":"workShopList",render: function (data){
						var html="";
						for(var i=0;i<data.length;i++){
							var processList=data[i].processList;
							if(i>0){
								html+="<br/><br/>"
							}
							html+="<button class='btn btn-warning radius pad'>"+data[i].workshopName+"</button> &nbsp;"
							for(var j=0;j<processList.length;j++){
								html+="<button class='btn btn-success radius pad'>"+processList[j].processName+"</button><i class='deleteX Hui-iconfont' hidden='hidden' value='"+processList[j].id+"'>&#xe60b;</i>&nbsp;"
							}
						}
						return html;
					}},
					{"data":"id",render: function (data) {
//						return "<button class='update btn btn-secondary radius ' type='button' value='"+data+"' >修改</button> " +
							return	"<button value='"+data+"'  class='delete btn btn-danger radius '>删除</button><br/><br/>"+
								"<button value='"+data+"'  class='remove btn btn-primary radius '>移除工序</button><br/><br/>"+
								"<button value='"+data+"'  class='update btn btn-warning radius  '>修改名称</button><br/><br/>"+
								"<button value='"+data+"'  class='add btn btn-success radius '>添加工序</button>";
					}}
			]);
		},
		//获取所有车间下的所有工序
		getAllWorkShopAndProcedure:function(){
			ajaxUtil.ajaxRuquest("orderProcedureTemplate/getAllWorkShopAndProcedure",{"type":0},function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					var proceList=data[i].processList;
					var workId=data[i].id;
					var workName=data[i].workshopName;
					for(var j=0;j<proceList.length;j++){
						html+="<li class='item ui-sortable-handle' value='"+workId+"' data-id="+proceList[j].id+">"+workName +"&nbsp;"+proceList[j].processName+"</li>"
					}
					html+="<br/>"
				}
				$(".left-box").append(html);
				orderProcedure.leftAndRightBind();
			});
		},
		leftAndRightBind:function(){
			$(".item").unbind("click");
			$(".left-box .item").unbind("dblclick");
			$(".right-box .item").unbind("dblclick");
			//单击
			$(".item").bind("click",function(){
				$(this).toggleClass("selected-item");
			});
			
			//左边双击右移
			$(".left-box .item").bind("dblclick",function(){
				$(".right-box").append($(this).removeClass('selected-item'));
				orderProcedure.leftAndRightBind();
			});
			
            //右边双击左移
			$(".right-box .item").bind("dblclick",function(){
				$(".left-box").append($(this).removeClass('selected-item'));
				orderProcedure.leftAndRightBind();
			});
		},
		removeBtnBind:function(){
			$(".add-one").bind("click",function(){
				$(".right-box").append($(".left-box").find('.selected-item').removeClass('selected-item'));
			});
			
			$(".add-all").bind("click",function(){
				$(".right-box").append($(".left-box").find('.item').removeClass('selected-item'));
			});
			$(".remove-one").bind("click",function(){
				$(".left-box").append($(".right-box").find('.selected-item').removeClass('selected-item'));
			});
			
			$(".remove-all").bind("click",function(){
				$(".left-box").append($(".right-box").find('.item').removeClass('selected-item'));
			});
		},
		remove:function(){
			$('.item-box').sortable({
				placeholder: 'item-placeholder',
				connectWith: '.item-box',
				revert: true
			}).droppable({
				accept: '.item',
				hoverClass: 'item-box-hover',
				drop: function(event, ui){
					setTimeout(function(){
						ui.draggable.removeClass('selected-item');
					}, 500);
				}
			}).disableSelection();
		},
		getTempNoHaveProcedure:function(){
			$('.addPorkShopCheck').unbind("click"); 
			$('.addProceCheck').unbind("click"); 
			ajaxUtil.ajaxRuquest("orderProcedureTemplate/getAllWorkShopAndProcedure",{"type":3,"tempId":orderProcedure.tempId},function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					var proceList=data[i].processList;
					html+="<input name='add"+i+"' type='checkbox' class='addPorkShopCheck' value='"+data[i].id+"'>"+data[i].workshopName+"</br>";
					for(var j=0;j<proceList.length;j++){
						html+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name='proceadd"+i+"'type='checkbox' value="+proceList[j].id+" class='addProceCheck'>"+proceList[j].processName+"<br/>";
					}
				}
				
				$("#addCheck").html("");
				$("#addCheck").append(html);
				orderProcedure.addCheckBind();
			});
		}
}

//setInterval ("table.ajax.reload(null, false)", 1000);//十五分钟更新一次数据
